package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    //新增到购物车
    @Override
    public Cart save(Cart cart) {
        //1.判断当前cart在数据表中是否存在(查询条件：套餐/菜品id   UserId)
        //select * from shopping_cart where dish_id = #{菜品id} and userId = #{登录用户id}
        //selset * from shopping_cart where setmeal_id = #{套餐id} and userId = #{登录用户id}
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId());
        wrapper.eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId());
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());

        //这个查询最多可以返回一条数据记录
        Cart cartFromDB = cartMapper.selectOne(wrapper);

        //判断基于上述条件的购物车在数据库中是否存在
        if (cartFromDB != null) {
            //如果存在，需要将此记录中的number+1，在更新回数据库
            cartFromDB.setNumber(cartFromDB.getNumber() + 1);
            cartMapper.updateById(cartFromDB);
            return cartFromDB;
        } else {
            //3.如果不在，需要构建一个cart对象，保存到数据库中
            cart.setUserId(UserHolder.get().getId());
            cart.setNumber(1);
            cart.setCreateTime(new Date());
            cartMapper.insert(cart);

            return cart;
        }
    }

    //查询购物车
    @Override
    public List<Cart> findList() {
        //查询当前用户的购物车
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        //select * from shopping cart where user id = #{登录用户id}
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        return cartMapper.selectList(wrapper);
    }

    //清空购物车
    @Override
    public void clean() {
        //delete from shopping_cart where user_id = #{登录用户id}
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        cartMapper.delete(wrapper);
    }

    //修改购物车
    @Override
    public Cart update(Cart cartPram) {
        //1.确定更新的购物车
        Long dishId = cartPram.getDishId();
        Long setmealId = cartPram.getSetmealId();

        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dishId != null, Cart::getDishId, dishId);
        wrapper.eq(setmealId != null, Cart::getSetmealId, setmealId);
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());

        Cart cart = cartMapper.selectOne(wrapper);

        //2.执行跟新/删除操作
        if (cart != null) {
            cart.setNumber(cart.getNumber() - 1);
            if (cart.getNumber() <= 0) {
                //删除
                cartMapper.deleteById(cart.getId());
            } else {
                //跟新
                cartMapper.updateById(cart);
            }
        }

        return cart;
    }
}
